package com.puge.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.puge.commonutils.R;
import com.puge.vod.service.VodService;
import com.puge.vod.utils.ConstantVodUtils;
import com.puge.vod.utils.InitVodCilent;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author by sun
 * @date 2021/6/24.12:53
 */
@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
@Component
public class VodController {

    @Autowired
    private VodService vodService;

//    上传视频到阿里云
    @PostMapping("uploadAliyunVideo")
    public R uploadAliyunVideo(MultipartFile file){
        //返回上传视频的id
        String videoId = vodService.uploadVideiAly(file);
        return R.ok().data("videoId",videoId);
    }

//    根据视频id删除阿里云视频：删除已经上传到阿里云的视频的接口
@ApiOperation("删除阿里云视频")
@DeleteMapping("removeAlyVideo/{id}")
    public  R removeAlyVideo(@PathVariable String id){
        try {
        //    初始化对象
            DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
        //   创建删除对象request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
        //    向request设置视频Id
            request.setVideoIds(id);
        //    调用初始化对象的方法实现删除
            client.getAcsResponse(request);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("删除失败");
            throw new PuGeException(20001,"删除视频失败");
        }
    }
            //    删除多个阿里云视频的方法
        //    参数为多个视频id
            @DeleteMapping("delete-batch")
        public R deleteBatch(@RequestParam("videoIdList")List videoIdList){
        vodService.removeMoreAlyVideo(videoIdList);
        return R.ok();
            }

}










